package com.alipay.sdk.util;

import android.view.KeyEvent;

public class ScanKeyUtil {

    private boolean mCaps;
    private StringBuilder mResult;
    public OnScanValueListener mListener;

    private long lastTime;
    private boolean runs;

    public interface OnScanValueListener {
        void onScanValue(String value);
    }

    public ScanKeyUtil(OnScanValueListener listener) {
        this.mListener = listener;
        this.mResult = new StringBuilder();
    }

    public void input(String keyCode) {
        String aChar = aliKeyValue(keyCode);
        if (aChar != null && !"".equals(aChar)) {
            mResult.append(aChar);
        }
        lastTime = System.currentTimeMillis();
        MyThread1 mt1 = new MyThread1();
//        new Thread(mt1).start();
        int code = Integer.parseInt(keyCode);
        if (code == 20) {

            if (mListener != null) {
                String result = mResult.toString();
                try {
                    mListener.onScanValue(result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            mResult.delete(0, mResult.length());
        }
        long nowTime = System.currentTimeMillis();
//            System.out.println(mResult.toString() + "当前时间：" + nowTime + "上次时间：" + lastTime + "相差时间：" + (nowTime - lastTime));
        lastTime = nowTime;
    }

    public String aliKeyValue(String keyCode) {
        System.out.println("获取Code值：" + keyCode);

        int code = Integer.parseInt(keyCode);
        if (code == 59) {
            mCaps = true;
            return "";
        }
        if (code >= 29 && code <= 55) {
            char c = (char) ((mCaps ? 'A' : 'a') + code - 29);
            mCaps = false;
            return String.valueOf(c);
        }

        String str = "";
        switch (keyCode) {
            case "7":
                str = mCaps ? ")" : "0";
                break;
            case "8":
                str = mCaps ? "!" : "1";
                break;
            case "9":
                str = mCaps ? "@" : "2";
                break;
            case "10":
                str = mCaps ? "#" : "3";
                break;
            case "11":
                str = mCaps ? "$" : "4";
                break;
            case "12":
                str = mCaps ? "%" : "5";
                break;
            case "13":
                str = mCaps ? "^" : "6";

                break;
            case "14":
                str = mCaps ? "&" : "7";
                break;
            case "15":
                str = mCaps ? "*" : "8";
                break;
            case "16":
                str = mCaps ? "(" : "9";
                break;
//            case KeyEvent.KEYCODE_NUMPAD_SUBTRACT:
//                return '-';
//            case KeyEvent.KEYCODE_MINUS:
//                return '_';
//            case KeyEvent.KEYCODE_EQUALS:
//                return '=';
//            case KeyEvent.KEYCODE_NUMPAD_ADD:
//                return '+';
//            case KeyEvent.KEYCODE_GRAVE:
//                return mCaps ? '~' : '`';
//            case KeyEvent.KEYCODE_BACKSLASH:
//                return mCaps ? '|' : '\\';
//            case KeyEvent.KEYCODE_LEFT_BRACKET:
//                return mCaps ? '{' : '[';
//            case KeyEvent.KEYCODE_RIGHT_BRACKET:
//                return mCaps ? '}' : ']';
//            case KeyEvent.KEYCODE_SEMICOLON:
//                return mCaps ? ':' : ';';
//            case KeyEvent.KEYCODE_APOSTROPHE:
//                return mCaps ? '"' : '\'';
//            case KeyEvent.KEYCODE_COMMA:
//                return mCaps ? '<' : ',';
//            case KeyEvent.KEYCODE_PERIOD:
//                return mCaps ? '>' : '.';
//            case KeyEvent.KEYCODE_SLASH:
//                return mCaps ? '?' : '/';
        }
        mCaps = false;
        return str;
    }


    private class MyThread1 extends Thread {

        //在run方法里复写需要进行的操作:卖票速度1s/张
        @Override
        public void run() {
            if (runs) {
                return;
            }
            runs = true;
            while (true) {
                long nowTime = System.currentTimeMillis();
                if (nowTime - lastTime > 100) {
                    break;
                }
            }
            String result = mResult.toString();
            try {
                mListener.onScanValue(result);
            } catch (Exception e) {
                e.printStackTrace();
            }
            mResult.delete(0, mResult.length());
            runs = false;
        }
    }
}
